source("functions.r")

# Nauja ideja: aproksimuojam ln S ir imam e^x
# generateRho_e1 (0, 1, 0.01, 1, 1, 1, 1, 1, 1, 1, 1000)
# generateRho_e1 (0, 0.5, 0.1, 1, 1, 1, 1, 1, 0.5, 0.5, 10000)


# generateRho1 (0, 1, 0.01, 1, 1, 1, 1, 1, 1, 1, 1000) 
# generateA1 (0, 1, 0.01, 1, 1, 1, 1, 1, 1, 1000) 

# test (0, 1.5, 0.1, 1, 1, 1, 1, 1, 1)

generateRho_e1 <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma, vRho, vVid) {
  
  print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Heston start ^^^^^^^^^^^^")
  
  # Susideliojame parametrus...
  Pr = vPr
  T = vT
  h = vh
  sigma = vsigma
  y0 = vy0
  s0 = vs0
  r = vr
  k = vk
  teta = vteta
  Vid = vVid
  rho = vRho
  n = (T-Pr)/h + 1
  
  x_ = seq (Pr, T, h)
  
  E <- 0
  for (i in 1:(n-1)) {
    E <- c(E, 0) 
  }
  
  for (j in 1:Vid) {
    y = y0
    Y_h = y0
    s = s0
    S_h = s0
    #X_h = log (s0)
    E[1] = E[1] + f(s)
    
    # Tolygiai pasiskirste dydziai W_t generavimui
    u <- runif((n), min=0, max=1)
    # Tolygiai pasiskirste dydziai B_t generavimui
    uy <- runif((n), min=0, max=1)
    
    for (i in 1:(n-1)) {
      
      X_h = Sad_X (log(s), y, h, u[i])
      Y_h = CIRad (y, h, sigma*sigma, uy[i])
      
      # Is nepriklausomu X ir Y gauname priklausomus X2 ir Y
      X2_h = Sad_rho_e (log(s), y, h, rho, sigma, X_h, Y_h)
      #print (S_h)    
      x = XD (X2_h, y, h, k, r, teta)
      s = exp(x)
      y = CIRD (Y_h, h, teta, k)
      
      
      #if (f(s) < 0)
      #  print ("Bliamba")
      
      E[i+1] = E[i+1] + f(s)
      
    } 
  }


  for (i in 1:(n)) {
    E[i] <- E[i] / Vid
    print (E[i])
  }
  
  print ("------------------------ Zemiau: tikros reiksmes")
  
  Etikras <- 0
  for (i in 1:(n)) {
    Etikras[i] = tikras_momentas_n_rho (s0, y0, 3, r, k, teta, sigma, rho, Pr + h*(i-1))
    print (Etikras[i])
    #Etikras2[i] = tikras_momentas_n (s0, y0, 2, r, k, teta, sigma, h*(i))
  }
  
  
  plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
  lines (x_, Etikras, col="green")
  #plot (x_, Etikras, type="l", xlab="t", ylab="Ef(S)", col="green")
  
}


test <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma) {

	# Susideliojame parametrus...
	Pr = vPr
	T = vT
	h = vh
	sigma = vsigma
	y0 = vy0
	s0 = vs0
	r = vr
	k = vk
	teta = vteta
	n = (T-Pr)/h + 1
		
	x_ = seq (Pr, T, h)

	Etikras <- 0
	Etikras2 <- 0

	for (i in 1:(n)) {
		Etikras[i] = tikras_momentas_n_rho (s0, y0, 3, r, k, teta, sigma, 0, Pr + h*(i-1))
		Etikras2[i] = tikras_momentas_n (s0, y0, 3, r, k, teta, sigma, Pr + h*(i-1))
	}
 

	#plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
	#lines (x_, Etikras, col="green")
	plot (x_, Etikras, type="l", xlab="t", ylab="Etikras(S)", col="green")
	lines (x_, Etikras2, col="red")
}

generateRho1 <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma, vRho, vVid) {

	print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Heston start ^^^^^^^^^^^^")

	# Susideliojame parametrus...
	Pr = vPr
	T = vT
	h = vh
	sigma = vsigma
	y0 = vy0
	s0 = vs0
	r = vr
	k = vk
	teta = vteta
	Vid = vVid
	rho = vRho
	n = (T-Pr)/h + 1
		
	x_ = seq (Pr, T, h)

	E <- 0
	for (i in 1:(n-1)) {
		E <- c(E, 0) 
	}

	for (j in 1:Vid) {
		y = y0
		Y_h = y0
		s = s0
		S_h = s0
		E[1] = E[1] + f(s)

		# Tolygiai pasiskirste dydziai W_t generavimui
		u <- runif((n), min=0, max=1)
		# Tolygiai pasiskirste dydziai B_t generavimui
		uy <- runif((n), min=0, max=1)

		for (i in 1:(n-1)) {

			S_h = Sad (s, y, h, u[i])
			Y_h = CIRad (y, h, sigma*sigma, uy[i])

			s = D (Sad_rho (s, y, h, rho, sigma, S_h, Y_h), h, r)
			y = CIRD (Y_h, h, teta, k)

			if (f(s) < 0)
				print ("Bliamba")

			E[i+1] = E[i+1] + f(s)
		} 
	}

	for (i in 1:(n)) {
		E[i] <- E[i] / Vid
	}

	Etikras <- 0
	for (i in 1:(n)) {
		Etikras[i] = tikras_momentas_n_rho (s0, y0, 3, r, k, teta, sigma, rho, Pr + h*(i-1))
		#Etikras2[i] = tikras_momentas_n (s0, y0, 2, r, k, teta, sigma, h*(i))
	}


	plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
	lines (x_, Etikras, col="green")

}


###########
#
# rho = 0
#

generateA1 <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma, vVid) {

	print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Heston start ^^^^^^^^^^^^")

	# Susideliojame parametrus...
	Pr = vPr
	T = vT
	h = vh
	sigma = vsigma
	y0 = vy0
	s0 = vs0
	r = vr
	k = vk
	teta = vteta
	Vid = vVid
	n = (T-Pr)/h + 1
		
	x_ = seq (Pr, T, h)

	E <- 0
	for (i in 1:(n-1)) {
		E <- c(E, 0) 
	}

	for (j in 1:Vid) {
		y = y0
		Y_h = y0
		s = s0
		S_h = s0
		E[1] = E[1] + f(s)

		# Tolygiai pasiskirste dydziai W_t generavimui
		u <- runif((n), min=0, max=1)
		# Tolygiai pasiskirste dydziai B_t generavimui
		uy <- runif((n), min=0, max=1)

		for (i in 1:(n-1)) {

			S_h = Sad (s, y, h, u[i])
			s = D (S_h, h, r)

			Y_h = CIRad (y, h, sigma*sigma, uy[i])
			y = CIRD (Y_h, h, teta, k)

			E[i+1] = E[i+1] + f(s)
			print ("E[i+1]")
			print (E[i+1])
		} 
	}

	for (i in 1:(n)) {
		E[i] <- E[i] / Vid
	}

	Etikras <- 0
	for (i in 1:(n)) {
		Etikras[i] = tikras_momentas_n (s0, y0, 3, r, k, teta, sigma, Pr + h*(i-1))
		#Etikras2[i] = tikras_momentas_n (s0, y0, 2, r, k, teta, sigma, h*(i))
	}


	plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
	lines (x_, Etikras, col="green")

}

generateA2 <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma, vVid) {

	print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Heston 4 ad start ^^^^^^^^^^^^")

	# Susideliojame parametrus...
	Pr = vPr
	T = vT
	h = vh
	sigma = vsigma
	y0 = vy0
	s0 = vs0
	r = vr
	k = vk
	teta = vteta
	Vid = vVid
	n = (T-Pr)/h + 1
		
	x_ = seq (Pr, T, h)

	E <- 0
	for (i in 1:(n-1)) {
		E <- c(E, 0) 
	}

	for (j in 1:Vid) {
		y = y0
		Y_h = y0
		s = s0
		S_h = s0
		E[1] = E[1] + f(s)

		# Tolygiai pasiskirste dydziai W_t generavimui
		u <- runif((n), min=0, max=1)
		# Tolygiai pasiskirste dydziai B_t generavimui
		uy <- runif((n), min=0, max=1)

		for (i in 1:(n-1)) {

			#S_h = Sad4 (D (s, h/2, r), y, h, u[i])
			#S_h = ad3 (D (s, h/2, r), y, h, sigma, u[i])
			S_h = Sad4c (D (s, h/2, r), y, h, u[i])

			s = D (S_h, h/2, r)

			Y_h = CIRad4 (CIRD(y, h/2, teta, k), h, sigma*sigma, uy[i])
			y = CIRD (Y_h, h/2, teta, k)

			E[i+1] = E[i+1] + f(s)
		} 
	}

	for (i in 1:(n)) {
		E[i] <- E[i] / Vid
	}

	Etikras <- 0
	for (i in 1:(n)) {
		Etikras[i] = tikras_momentas_n (s0, y0, 3, r, k, teta, sigma, Pr + h*(i-1))
		#Etikras2[i] = tikras_momentas_n (s0, y0, 2, r, k, teta, sigma, h*(i))
	}

	plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
	lines (x_, Etikras, col="green")

}
#generateB (0, 10, 0.1, 1, 1, 0, 1, 1, 1, 1000)
generateB <- function (vPr, vT, vh, vs0, vy0, vr, vk, vteta, vsigma, vVid) {

	print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Heston kitos funkcijos start ^^^^^^^^^^^^")

	# Susideliojame parametrus...
	Pr = vPr
	T = vT
	h = vh
	sigma = vsigma
	y0 = vy0
	s0 = vs0
	r = vr
	k = vk
	teta = vteta
	Vid = vVid
	n = (T-Pr)/h + 1
		
	x_ = seq (Pr, T, h)

	E <- 0
	for (i in 1:(n-1)) {
		E <- c(E, 0) 
	}

	for (j in 1:Vid) {
		y = y0
		Y_h = y0
		s = s0
		S_h = s0
		E[1] = E[1] + f(s)

		# Tolygiai pasiskirste dydziai W_t generavimui
		u <- runif((n), min=0, max=1)
		# Tolygiai pasiskirste dydziai B_t generavimui
		uy <- runif((n), min=0, max=1)

		for (i in 1:(n-1)) {

			S_h = Sad (s, y, h, u[i])
			s = D (S_h, h, r)

			Y_h = CIRad (y, h, sigma*sigma, uy[i])
			y = CIRD (Y_h, h, teta, k)

			E[i+1] = E[i+1] + f(s)
		} 
	}

	for (i in 1:(n)) {
		E[i] <- E[i] / Vid
	}

	Etikras <- 0
	for (i in 1:(n)) {
		Etikras[i] = tikras_momentas_log (s0, y0, k, teta, Pr + h*(i-1))
	}

	plot (x_, E, type="l", xlab="t", ylab="Ef(S)", col="red")
	lines (x_, Etikras, col="green")

}


